<template>
  <div class="alert">
     
    <div class="alert-main" v-for="item in notices" :key="item.name">
        
      <div class="alert-content">{{ item.content }}</div>
       
    </div>
  </div>
</template>
<script>
  let seed = 0

  function getUuid() {
    return 'alert_' + (++seed)
  }

  export default {
    name: 'Alert',
    data() {
      return {
        notices: [] // 维护通知list
      }
    },
    methods: {
      add(notice) {
        const name = getUuid()
        let _notice = Object.assign({ name: name }, notice)
        this.notices.push(_notice)

        const duration = notice.duration
        setTimeout(() => {
          this.remove(name)
        }, duration * 1000 || 2000)
      },
      remove(name) {
        const index = this.notices.findIndex(item => item.name === name)
        this.notices.splice(index, 1)
      }
    }
  }
</script>
<style scoped rel="stylesheet/stylus">
  .alert {
    position: fixed;
    width: 100%;
    top: 18px;
    left: 0;
    right: 0;
    text-align: center;
  }

  .alert-content {
    display: inline-block;
    padding: 8px 16px;
    background: #fff;
    border-radius: 3px;
    box-shadow: 0 1px 6px rgba(0, 0, 0, .2);
    margin-bottom: 8px;
  }

</style>
